﻿using BBICMS.Model;
using BBICMS.Model.ViewModel.Back;
using BBICMS.Repository;
using BBICMS.WebUI.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BBICMS.WebUI.Areas.Admin.Controllers
{
     [SIGAuth]
    
    public class LinkController : Controller
    {
        private ApplicationUnit _unit = new ApplicationUnit();

        #region 链接

        //
        // GET: /Admin/Link/
       
        public ActionResult Index(int? page, int? categoryId)
        {
            //IEnumerable<Link> menuList = _unit.Links.GetAll().OrderByDescending(m => m.Importance)
            //    .ThenByDescending(m => m.AddedDate);

            IQueryable<Link> lLinks = _unit.Links.GetAll();

            if (categoryId > 0)
                lLinks = lLinks.Where(p => p.CategoryId == categoryId);

            LinkListVM LinkList = new LinkListVM
            {
                Links = lLinks.OrderByDescending(s => s.Importance).ThenByDescending(l=>l.AddedDate),
                CategoryId = categoryId ?? 0,
                Categories = GetCategoryList(categoryId),
                PageSize = BBICMS.Infrastructure.Helpers.Settings.PageSize,
                PageNumber = page ?? 1
            };


            return View(LinkList);
        }


       

        public ActionResult Add()
        {
            Link vLink = new Link();
            vLink.Active = true;
            vLink.Importance = 0;

            var categories = _unit.LinkCategories.GetAll().OrderByDescending(c=>c.Importance).ToList();
            var lCategories = new SelectList(categories, "Id", "Title");

            ViewBag.Categories = lCategories;

            return View("Add", vLink);
        }


        [HttpPost]
       
        public JsonResult Add(Link menu)
        {



            if (ModelState.IsValid)
            {
                _unit.Links.Add(menu);
                _unit.SaveChanges();

                // TempData["message"] = string.Format("{0} 已经保存", carousel.Title);
                return Json(true, JsonRequestBehavior.DenyGet);

            }
            else
            {
                return Json(false, JsonRequestBehavior.DenyGet);
            }
            //   return RedirectToAction("Index");

        }

       
        public ActionResult Edit(int Id)
        {
            var vLink = _unit.Links.GetById(Id);
            if (vLink != null)
            {
                var categories = _unit.LinkCategories.GetAll().OrderByDescending(c => c.Importance).ToList();
                var lCategories = new SelectList(categories, "Id", "Title");

                ViewBag.Categories = lCategories;

                return View(vLink);
            }
            return View("NotFound");
        }

        [HttpPost]
       
        public JsonResult Edit(Link Link)
        {


            //carousel.UpdatedBy = User.Identity.Name;
            //carousel.UpdatedDate = DateTime.Now;   

            if (ModelState.IsValid)
            {
                _unit.Links.Update(Link);
                _unit.SaveChanges();
                return Json(true, JsonRequestBehavior.DenyGet);

            }
            else
            {
                return Json(false, JsonRequestBehavior.DenyGet);
            }

        }

        // DELETE: /User/DeleteLink
        [HttpDelete]
       
        public JsonResult Delete(int id)
        {

            Link vLink = _unit.Links.GetById(id);

            if (vLink != null)
            {
                //string imgurl = Server.MapPath(vLink.ImageUrl);
                //if (System.IO.File.Exists(imgurl))
                //{
                //    System.IO.File.Delete(imgurl);    //删除缩略图
                //}

                _unit.Links.Delete(vLink);
                _unit.SaveChanges();
                return Json(true, JsonRequestBehavior.DenyGet);
            }

            return Json(false, JsonRequestBehavior.DenyGet);
        }

#endregion


        #region 链接分类


       
        public ActionResult Categories(int? page)
        {
            //log = log4net.LogManager.GetLogger("File");
            // log.Info(string.Format("The user: {0} add Company to DB  [id:{1},header:{2}]", "sharon", "ddddd", "title"));

            //log4net.ILog log = log4net.LogManager.GetLogger(this.GetType());
            //log.Info("Test message for Log4Net"); 

            LinkCategoryListVM LinkCategoryList = new LinkCategoryListVM
            {
                LinkCategories = _unit.LinkCategories.GetAll().OrderByDescending(s => s.Importance),
                PageSize = BBICMS.Infrastructure.Helpers.Settings.PageSize,
                PageNumber = page ?? 1
            };

            return View(LinkCategoryList);

            
        }

       
        public ActionResult AddCategory()
        {
            LinkCategory vCategory = new LinkCategory();
            vCategory.Active = true;
            vCategory.Importance = 0;

            return View(vCategory);
        }


        [HttpPost]
       
        public JsonResult AddCategory(LinkCategory category)
        {       

            if (ModelState.IsValid)
            {
                _unit.LinkCategories.Add(category);
                _unit.SaveChanges();

                // TempData["message"] = string.Format("{0} 已经保存", site.Title);
                return Json(true, JsonRequestBehavior.DenyGet);

            }
            else
            {
                return Json(false, JsonRequestBehavior.DenyGet);
            }
            //   return RedirectToAction("Index");

        }


       
        public ActionResult EditCategory(int Id)
        {
            var vCategory = _unit.LinkCategories.GetById(Id);
            if (vCategory != null)
            {
                return View(vCategory);
            }
            return View("NotFound");
        }

        [HttpPost]

        public JsonResult EditCategory(LinkCategory category)
        {


            //site.UpdatedBy = User.Identity.Name;
            //site.UpdatedDate = DateTime.Now;   

            if (ModelState.IsValid)
            {
                _unit.LinkCategories.Update(category);
                _unit.SaveChanges();
                return Json(true, JsonRequestBehavior.DenyGet);

            }
            else
            {
                return Json(false, JsonRequestBehavior.DenyGet);
            }

        }

        // DELETE: /User/DeleteSite
        [HttpDelete]
        public JsonResult DeleteCategory(int id)
        {

            int linkCount = _unit.Links.GetAll().Where(l => l.CategoryId == id).Count();
            if (linkCount > 0)
                return Json(false, JsonRequestBehavior.DenyGet);


            LinkCategory vCategory = _unit.LinkCategories.GetById(id); 

            if (vCategory != null)
            {
                //string imgurl = Server.MapPath(vBlog.ImageURL);
                //if (System.IO.File.Exists(imgurl))
                //{
                //    System.IO.File.Delete(imgurl);    //删除缩略图
                //}       

                _unit.LinkCategories.Delete(vCategory);
                _unit.SaveChanges();
                return Json(true, JsonRequestBehavior.DenyGet);
            }

            return Json(false, JsonRequestBehavior.DenyGet);
        }

        public List<SelectListItem> GetCategoryList(int? categoryId)
        {
            var categories = _unit.LinkCategories.GetAll().OrderByDescending(b => b.Importance).ThenByDescending(b => b.AddedDate).ToList();
            List<SelectListItem> lCategories = new List<SelectListItem>();
            foreach (var category in categories)
            {
                SelectListItem sli = new SelectListItem();
                sli.Text = category.Title;
                sli.Value = category.Id.ToString();
                sli.Selected = (categoryId == category.Id);

                lCategories.Add(sli);

            }
            lCategories.Insert(0, new SelectListItem { Text = "--按分类过滤--", Value = "" });

            return lCategories;
        }
        #endregion

    }
}
